package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.TourImageDAO;
import bist.believeinstep.domain.TourImage;
import bist.believeinstep.mapper.TourImageMapper;

public class TourImageTemplate implements TourImageDAO, ConnectionDAO {
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void create(TourImage tourImage){
		String query = "INSERT INTO bist_tour_images (bist_tours_id, bist_images_id,"
				+ "created_date, modified_date, created_by, modified_by) VALUES (?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(query, new Object[]{
				tourImage.getTourId(),
				tourImage.getImageId(),
				tourImage.getCreated(),
				tourImage.getModified(),
				tourImage.getCreatedBy(),
				tourImage.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(TourImage tourImage){
		String query = "UPDATE bist_tour_images SET"
				+ "bist_tours_id = ?"
				+ "bist_tours_images_id = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "modified_by = ?";
		try{
			jdbcTemplate.update(query, new Object[]{
				tourImage.getTourId(),
				tourImage.getImageId(),
				tourImage.getCreated(),
				tourImage.getModified(),
				tourImage.getCreatedBy(),
				tourImage.getModifiedBy()		
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public TourImage getTourImage(Integer id){
		String query = "SELECT FROM bist_tour_images WHERE id = ?";
		try{
			TourImage tourImage = jdbcTemplate.queryForObject(query, new Object[]{id}, new TourImageMapper());
			return tourImage;
		}catch(Exception ex){
			ex.printStackTrace();
		}	
		return null;
	}
	@Override
	public TourImage getTourImage(String imageId){
		String query = "SELECT FROM bist_tour_images WHERE bist_images_id = ?";
		try{
			TourImage tourImage = jdbcTemplate.queryForObject(query, new Object[]{imageId}, new TourImageMapper());
			return tourImage;
		}catch(Exception ex){
			ex.printStackTrace();
		}	
		return null;
	}
	@Override
	public List<TourImage> listTourImage(){
		String query = "SELECT * FROM bist_tour_images";
		try{
			List<TourImage> tourImage = jdbcTemplate.query(query, new TourImageMapper());
			return tourImage;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bist_tour_images WHERE id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(String imageId){
		String query = "DELETE FROM bist_tour_images WHERE bist_image_id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{imageId});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
